package com.elitel.frame.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.elitel.common.utils.RedisUtils;
import com.elitel.common.utils.StringUtils;
import com.elitel.frame.base.service.Server2CacheService;
import com.elitel.frame.base.service.SysPrjServiceService;
import com.elitel.frame.main.dao.AcAppMapper;
import com.elitel.frame.main.dao.AcAppServiceMapper;
import com.elitel.frame.main.entity.AcApp;
import com.elitel.frame.main.entity.AcAppService;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.PrjServiceExt;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.elitel.common.constant.RedisKeyConstant.SERVER_INFO_KEY;

/**
 * @author mulis
 */
@Service
public class Server2CacheServiceImpl implements Server2CacheService {


    @Resource
    private SysPrjServiceService sysPrjServiceService;
    @Resource
    private AcAppMapper acAppMapper;
    @Resource
    private AcAppServiceMapper acAppServiceMapper;


    private static final String APP_INFO_KEY = "dscloud:config:appinfo";
    private static final String AC_APP_SERVICE_PRE_KEY = "dscloud:config:acappservice:";
    public static final String GUID_PATH_MAPPING_KEY = "dscloud:config:guidpathmapping";


    /**
     * 将服务基本信息保存到缓存
     */
    @Override
    public void saveServerInfo2Cache() {
        int currentPage = 0;
        int pageSize = 10000;
        int totalPage;
        RedisUtils.delete(SERVER_INFO_KEY);
        RedisUtils.delete(GUID_PATH_MAPPING_KEY);
        PageRequestVo<SysPrjservice> params = new PageRequestVo<>();
        // 1. 分页查询数据
        do {
            params.setPageNo(++currentPage);
            params.setPageSize(pageSize);
            PageResponseVo<SysPrjservice> tableData = sysPrjServiceService.getSysProjectServiceList(params);
            totalPage = (tableData.getTotal() / pageSize) + (tableData.getTotal() % pageSize > 0 ? 1 : 0);
            // 2. 循环遍历，将数据保存到缓存中
            List rows = tableData.getRows();
            for (Object row : rows) {
                PrjServiceExt cRow = (PrjServiceExt) row;
                RedisUtils.hPut(SERVER_INFO_KEY, cRow.getGuid(), JSONObject.toJSONString(cRow));
                if (StringUtils.isNotEmpty(cRow.getApiPath())) {
                    RedisUtils.hPut(GUID_PATH_MAPPING_KEY, cRow.getApiPath(), cRow.getGuid());
                }
            }
        } while (totalPage > currentPage);
    }

    /**
     * 将调用方和服务精细化信息保存到缓存
     */
    @Override
    public void saveAppInfo2Cache() {
        int currentPage = 0;
        int pageSize = 100;
        long totalPage;
        RedisUtils.delete(APP_INFO_KEY);
        do {
            PageHelper.startPage(++currentPage, pageSize);
            List<AcApp> list = acAppMapper.selectByPrimaryKeys(null);
            for (AcApp acApp : list) {
                RedisUtils.hPut(APP_INFO_KEY, acApp.getAppCode(), JSONObject.toJSONString(acApp));
            }
            PageInfo<AcApp> tableData = new PageInfo<>(list);
            long total = tableData.getTotal();
            totalPage = (total / pageSize) + (total % pageSize > 0 ? 1 : 0);
        } while (totalPage > currentPage);
    }

    /**
     * 将调用方和服务精细化信息保存到缓存
     */
    @Override
    public void saveAppServerInfo2Cache() {
        int currentPage = 0;
        int pageSize = 100;
        long totalPage;
        RedisUtils.delete(AC_APP_SERVICE_PRE_KEY);
        do {
            PageHelper.startPage(++currentPage, pageSize);
            List<AcAppService> acAppServices = acAppServiceMapper.selectAppServiceList();
            for (AcAppService acAppService : acAppServices) {
                RedisUtils.hPut(AC_APP_SERVICE_PRE_KEY + acAppService.getAppCode(), acAppService.getServiceId(), JSONObject.toJSONString(acAppService));
            }
            PageInfo<AcAppService> tableData = new PageInfo<>(acAppServices);
            long total = tableData.getTotal();
            totalPage = (total / pageSize) + (total % pageSize > 0 ? 1 : 0);
        } while (totalPage > currentPage);
    }
}
